home *** CD-ROM | disk | FTP | other *** search
/ Essentials of Interactive Physiology / Essentials of Interactive Physiology.iso / pc / files / code / ipcookie.js < prev    next >
Encoding:
Text File  |  2000-09-08  |  6.4 KB  |  175 lines

  1.  
  2. // ----------------------------- IP BACK BUTTON CODE ------------------------------
  3. // cookie is ,<system>/<topic>/<pagenum>,<system>/<topic>/<pagenum>,etc.
  4. // When the media page loads, the current system/topic/pagenum is added to the cookie
  5. // handle the back button click on the navbar 
  6. function handleBackBtnClick() {
  7.     // get the current cookie value and length of trail
  8.      vsCookieValue = getCookie("IPTrail");
  9.      vsTrailLength = getNumberOfItemsFromDelimitedString(vsCookieValue, ",");
  10.      
  11.      //alert("BEFORE - length = " + vsTrailLength + ", value = " + vsCookieValue);
  12.           
  13.      // if the cookie exists
  14.      if(vsTrailLength >= 3) {
  15.          // strip off the last (which is the current string)
  16.          vsStrippedCookie = stripLastItemFromDelimitedString(vsCookieValue, ",");
  17.                   
  18.          // get get last of stripped string
  19.          vsParamsToGoTo = getLastItemFromDelimitedString(vsStrippedCookie, ",");
  20.      
  21.          // take another off since the page you're going to will add itself to the cookie
  22.          vsStrippedCookie = stripLastItemFromDelimitedString(vsStrippedCookie, ",");
  23.          
  24.          // reset the cookie with the last item off
  25.          setCookie("IPTrail", vsStrippedCookie);
  26.  
  27.          if(vsParamsToGoTo != "") {
  28.              top.document.location = "buildframes.html?" + vsParamsToGoTo;
  29.          } 
  30.      }
  31. }
  32.  
  33. // appends the current location to the IPTrail cookie
  34. // creates it if necessary
  35. function addToTrailCookie() { 
  36.     vsToAdd = parent.gsSystem + "/" + parent.gsTopic + "/" + parent.gsPageNum;
  37.      vsCurValue = getCookie("IPTrail");
  38.  
  39.      // if the cookie is null, add preceding comma
  40.     if(vsCurValue == null) {
  41.         vsToWrite = "," + vsToAdd;
  42.     } else {
  43.         vsToWrite     = vsCurValue + "," + vsToAdd;
  44.     }
  45.     
  46.     setCookie("IPTrail", vsToWrite);
  47.     
  48.     //alert("Cookie to add: " + vsToAdd);
  49.     //alert("Added. Now cookie = " + getCookie("IPTrail"));
  50. }
  51.  
  52. function deleteIPTrailCookie() {
  53.     deleteCookie("IPTrail");
  54. }
  55.  
  56. // ----------------------------- IP SOUND COOKIE CODE ------------------------------
  57. // writes a cookie named "IP" with the value xsValue
  58. function setIPSndCookie(xsValue) { 
  59.    var today = new Date();
  60.    var expires = new Date();
  61.    FixCookieDate (expires);
  62.    expires.setTime(today.getTime() + 1000*60*60*24*365) ;// one year
  63.    setCookie("IPSnd", xsValue, expires, "/");
  64. }
  65.  
  66. function deleteIPSndCookie() {
  67.     deleteCookie("IPSnd");
  68. }
  69.  
  70. // returns whether or not the sound is on (string "true" or "false" or null)
  71. // null if no cookie
  72. function narrationOnP() {
  73.     vsNarrationOn = getCookie("IPSnd");
  74.       
  75.     return vsNarrationOn; 
  76. }
  77.  
  78. // ------------------------------ GENERAL COOKIE CODE -----------------------------
  79. //  Function to create or update a cookie.
  80. //    name - String object containing the cookie name.
  81. //    value - String object containing the cookie value.  May contain
  82. //      any valid string characters.
  83. //    [expires] - Date object containing the expiration data of the cookie.  If
  84. //      omitted or null, expires the cookie at the end of the current session.
  85. //    [path] - String object indicating the path for which the cookie is valid.
  86. //      If omitted or null, uses the path of the calling document.
  87. //    [domain] - String object indicating the domain for which the cookie is
  88. //      valid.  If omitted or null, uses the domain of the calling document.
  89. //    [secure] - Boolean (true/false) value indicating whether cookie transmission
  90. //      requires a secure channel (HTTPS).  
  91. //
  92. //  The first two parameters are required.  The others, if supplied, must
  93. //  be passed in the order listed above.  To omit an unused optional field,
  94. //  use null as a place holder.  For example, to call SetCookie using name,
  95. //  value and path, you would code:
  96. //
  97. //      SetCookie ("myCookieName", "myCookieValue", null, "/");
  98. //
  99. //  Note that trailing omitted parameters do not require a placeholder.
  100. //
  101. //  To set a secure cookie for path "/myPath", that expires after the
  102. //  current session, you might code:
  103. //
  104. //      SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
  105. function setCookie (name,value,expires,path,domain,secure) {
  106.   document.cookie = name + "=" + escape (value) +
  107.     ((expires) ? "; expires=" + expires.toGMTString() : "") +
  108.     ((path) ? "; path=" + path : "") +
  109.     ((domain) ? "; domain=" + domain : "") +
  110.     ((secure) ? "; secure" : "");
  111. }
  112.  
  113.  
  114. //-----------------------
  115. //  Function to return the value of the cookie specified by "name".
  116. //    name - String object containing the cookie name.
  117. //    returns - String object containing the cookie value, or null if
  118. //      the cookie does not exist.
  119. function getCookie (name) {
  120.   var arg = name + "=";
  121.   var alen = arg.length;
  122.   var clen = document.cookie.length;
  123.   var i = 0;
  124.   while (i < clen) {
  125.     var j = i + alen;
  126.     if (document.cookie.substring(i, j) == arg) {
  127.       return getCookieVal(j);
  128.     }
  129.     i = document.cookie.indexOf(" ", i) + 1;
  130.     if (i == 0) break; 
  131.   }
  132.  
  133.   return null;
  134. }
  135.  
  136. //-----------------------
  137. // "Internal" function to return the decoded value of a cookie
  138. function getCookieVal (offset) {
  139.   var endstr = document.cookie.indexOf (";", offset);
  140.   if (endstr == -1)
  141.     endstr = document.cookie.length;  
  142.   return unescape(document.cookie.substring(offset, endstr));
  143. }
  144.  
  145. //-----------------------
  146. //  Function to delete a cookie. (Sets expiration date to start of epoch)
  147. //    name -   String object containing the cookie name
  148. //    path -   String object containing the path of the cookie to delete.  This MUST
  149. //             be the same as the path used to create the cookie, or null/omitted if
  150. //             no path was specified when creating the cookie.
  151. //    domain - String object containing the domain of the cookie to delete.  This MUST
  152. //             be the same as the domain used to create the cookie, or null/omitted if
  153. //             no domain was specified when creating the cookie.
  154. function deleteCookie(name, path, domain) {
  155.   if (getCookie(name)) {
  156.     document.cookie = name + "=" + 
  157.     ((path) ? "; path=" + path : "") +
  158.     ((domain) ? "; domain=" + domain : "") +
  159.     "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  160.   }
  161. }
  162.  
  163.  
  164. //-----------------------
  165. //  Function to correct for 2.x Mac date bug.  Call this function to
  166. //  fix a date object prior to passing it to SetCookie.
  167. //  IMPORTANT:  This function should only be called *once* for
  168. //  any given date object!  See example at the end of this document.
  169. function FixCookieDate (date) {
  170.   var base = new Date(0);
  171.   var skew = base.getTime(); // dawn of (Unix) time - should be 0
  172.   if (skew > 0)  // Except on the Mac - ahead of its time
  173.     date.setTime (date.getTime() - skew);
  174. }
  175.